package com.base.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.po.sqlparam.OperationlogsExamplePage;
import com.base.po.voip.Operationlogs;
import com.base.po.voip.Users;
import com.base.po.voip.OperationlogsExample.Criteria;
import com.base.service.LogsService;
import com.base.service.UsersService;
import com.base.utils.Page;

/**
 * admin管理系统控制器
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class content_caozuorizhi {
	@Autowired
	private LogsService logsService;
	@Autowired
	private UsersService usersService;

	// 操作日志
	@RequestMapping("/content_caozuorizhiOld")
	public ModelAndView indexOld(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		OperationlogsExamplePage examplePage = new OperationlogsExamplePage();
		examplePage.pagesize = 10;
		examplePage.start = 0;
		examplePage.rows = examplePage.pagesize;// 设置首次分页
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		List<Operationlogs> operationlogs = null;
		Users user = null;
		Criteria criteria = examplePage.createCriteria();
		if (adminUsergrade == 0) {
			examplePage.setOrderByClause("CreateTime");
			operationlogs = logsService.selectByExamplePage(examplePage);
		}
		if (adminUsergrade == 1 || adminUsergrade == 2) {
			user = usersService.selectByPrimaryKey(adminUserId);
			examplePage.setOrderByClause("CreateTime");
			examplePage.getOredCriteria().add(criteria.andNameEqualTo(user.getUsername()));
			operationlogs = logsService.selectByExamplePage(examplePage);
		}
		/*
		 * 设置分页
		 */
		int totlenum = logsService.countByExample(examplePage);// 页面总数
		request.setAttribute("totlenum", totlenum);// 传入session中
		request.setAttribute("curPage", 1);// 当前页面传入session中
		if (operationlogs != null) {
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_caozuorizhi");
			request.setAttribute("auth", 4);
			request.setAttribute("list", operationlogs);
			return modelAndView;
		}
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}

	// 操作日志
	@RequestMapping("/selectLogsOld")
	public ModelAndView selectLogsOld(HttpServletRequest request, Integer datastatus, Integer curPage) throws Exception {
		// 设置session缓存
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
		if (datastatus == null) {
			if (request.getSession().getAttribute("datastatus") != null) {
				datastatus = (Integer) request.getSession().getAttribute("datastatus");
			} else {
				datastatus = 0;
			}
		}
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		OperationlogsExamplePage examplePage = new OperationlogsExamplePage();
		examplePage.pagesize = 10;
		examplePage.start = (curPage - 1) * examplePage.pagesize;
		examplePage.rows = examplePage.pagesize;// 设置首次分页
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		List<Operationlogs> operationlogs = null;
		Users user = null;
		Criteria criteria = examplePage.createCriteria();
		if (datastatus != null) {
			// 判断当前可用性
			switch (datastatus) {
			case 2:
				examplePage.getOredCriteria().add(criteria.andDatestatusEqualTo(false));
				break;
			case 1:
				examplePage.getOredCriteria().add(criteria.andDatestatusEqualTo(true));
				break;
			case 0:
				break;
			}
		}
		request.setAttribute("datastatus", datastatus);
		if (adminUsergrade == 0) {
			examplePage.setOrderByClause("CreateTime");
			operationlogs = logsService.selectByExamplePage(examplePage);
		}
		if (adminUsergrade == 1 || adminUsergrade == 2) {
			user = usersService.selectByPrimaryKey(adminUserId);
			examplePage.setOrderByClause("CreateTime");
			examplePage.getOredCriteria().add(criteria.andNameEqualTo(user.getUsername()));
			operationlogs = logsService.selectByExamplePage(examplePage);
		}
		/*
		 * 设置分页
		 */
		int totlenum = logsService.countByExample(examplePage);// 页面总数
		request.setAttribute("totlenum", totlenum);// 总数据传入session中
		request.setAttribute("curPage", curPage);// 当前页面传入session中
		request.setAttribute("auth", datastatus);
		if (operationlogs != null) {
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_caozuorizhi");
			request.setAttribute("list", operationlogs);
			return modelAndView;
		}
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}

	@RequestMapping("/content_caozuorizhi")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		Page page = new Page();
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		List<Operationlogs> operationlogs = null;
		Users user = null;
		if (adminUsergrade == 0) {
			page.setCurrentPageNo(1);
			page.setRecordTotal(logsService.countByAll(null, null, null, null));
			operationlogs = logsService.selectByAll(page, null, null, null, null);
		}
		if (adminUsergrade == 1 || adminUsergrade == 2) {
			user = usersService.selectByPrimaryKey(adminUserId);
			page.setCurrentPageNo(1);
			page.setRecordTotal(logsService.countByName(user.getUsername(), null, null, null));
			operationlogs = logsService.selectByName(page, user.getUsername(), null, null, null);
		}
		/*
		 * 设置分页
		 */
		int totlenum = page.getRecordTotal();// 页面总数
		request.setAttribute("totlenum", totlenum);// 传入session中
		request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中
		if (operationlogs != null) {
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_caozuorizhi");
			request.setAttribute("list", operationlogs);
			return modelAndView;
		}
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
	
	@RequestMapping("/selectLogs")
	public ModelAndView selectLogs(HttpServletRequest request, HttpServletResponse response,
			String name, String ipinfo, String timeFrom, String timeTo, Integer curPage) throws Exception {
		// 设置session缓存
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
		// 返回ModelAndView
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		ModelAndView modelAndView = new ModelAndView();
		PrintWriter out = response.getWriter();
		Page page = new Page();
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		List<Operationlogs> operationlogs = null;
		Users user = null;
		
		String fName = null;
		String fIpinfo = null;
		String fTimeFrom = null;
		String fTimeTo = null;
		if(name != null && !name.equals("")) {
			fName = name;
		}
		if(ipinfo != null && !ipinfo.equals("")) {
			fIpinfo = ipinfo;
		}
		if(timeFrom !=null && !timeFrom.equals("") && timeTo != null && !timeTo.equals("")) {
			fTimeFrom = timeFrom + " 00:00:00";
			fTimeTo = timeTo + " 23:59:59";
		}else {
			out.print("请填写时间段");
		}	
		if (adminUsergrade == 0) {
			page.setCurrentPageNo(curPage);
			page.setRecordTotal(logsService.countByAll(fIpinfo, fName, fTimeFrom, fTimeTo));
			operationlogs = logsService.selectByAll(page, fIpinfo, fName, fTimeFrom, fTimeTo);
		}
		if (adminUsergrade == 1 || adminUsergrade == 2) {
			user = usersService.selectByPrimaryKey(adminUserId);
			page.setCurrentPageNo(curPage);
			page.setRecordTotal(logsService.countByName(user.getUsername(), fIpinfo, fTimeFrom, fTimeTo));
			operationlogs = logsService.selectByName(page, user.getUsername(), fIpinfo, fTimeFrom, fTimeTo);
		}
		/*
		 * 设置分页
		 */
		int totlenum = page.getRecordTotal();// 页面总数
		request.setAttribute("totlenum", totlenum);// 总数据传入session中
		request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中
		if (operationlogs != null) {
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_caozuorizhi");
			request.setAttribute("name", name);
			request.setAttribute("ipinfo", ipinfo);
			request.setAttribute("list", operationlogs);
			request.setAttribute("timeFrom", timeFrom);
			request.setAttribute("timeTo", timeTo);
			return modelAndView;
		}
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
}
